package com.example.jpetstoreback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.jpetstoreback.common.CommonResponse;
import com.example.jpetstoreback.entity.Employee;
import com.example.jpetstoreback.persistence.EmployeeMapper;
import com.example.jpetstoreback.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public CommonResponse<Employee> getAccountByEmpIdAndPwd(String empId, String password) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("empid", empId);
        //生成加密后的密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        queryWrapper.eq("password", password);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if(employee != null){
            return CommonResponse.createForSuccess(employee);
        }
        return CommonResponse.createForError("用户不存在！");
    }

    @Override
    public CommonResponse<Employee> getAccountByPhoneAndCode(String phone) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if(employee != null){
            return CommonResponse.createForSuccess(employee);
        }
        return CommonResponse.createForError("用户不存在！");
    }
}
